﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace Tools.CryptoService
{
    public class Encrypt
    {
        /// <summary>
        /// Url加密
        /// </summary>
        /// <param name="str"></param>
        /// <param name="en"></param>
        /// <returns></returns>
        public static string UrlEncode(string str, Encoding en)
        {
            if (str.Trim().Equals("")) return str;
            var sb = new StringBuilder();
            byte[] byStr = en.GetBytes(str);
            foreach (var t in byStr)
            {
                sb.Append(@"%" + Convert.ToString(t, 16));
            }

            return (sb.ToString());
        }

        /// <summary>
        /// MD5　32位加密
        /// </summary>
        public static string EncryptMd5(string input, Encoding encoding = null)
        {
            var md5 = new MD5CryptoServiceProvider();
            encoding = encoding ?? Encoding.GetEncoding("GB2312");
            byte[] inBytes = encoding.GetBytes(input);
            byte[] outBytes = md5.ComputeHash(inBytes);
            string outString = "";
            for (int i = 0; i < outBytes.Length; i++)
            {
                outString += outBytes[i].ToString("x2");
            }
            return outString;
        }
    }
}
